package cn.iocoder.yudao.module.infra.convert.redis;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.redis.core.RedisKeyDefine;
import cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisKeyDefineRespVO;
import cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisMonitorRespVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * redis转换
 *
 * @author huangweidong
 * @date 2023/05/09
 */
@Mapper
public interface RedisConvert {

    RedisConvert INSTANCE = Mappers.getMapper(RedisConvert.class);

    /**
     * 构建
     *
     * @param info         信息
     * @param dbSize       数据库大小
     * @param commandStats 命令数据
     * @return {@code RedisMonitorRespVO}
     */
    default RedisMonitorRespVO build(Properties info, Long dbSize, Properties commandStats) {
        RedisMonitorRespVO respVO = RedisMonitorRespVO.builder().info(info).dbSize(dbSize)
                .commandStats(new ArrayList<>(commandStats.size())).build();
        commandStats.forEach((key, value) -> respVO.getCommandStats().add(RedisMonitorRespVO.CommandStat.builder()
                .command(StrUtil.subAfter((String) key, "cmdstat_", false))
                .calls(Long.valueOf(StrUtil.subBetween((String) value, "calls=", ",")))
                .usec(Long.valueOf(StrUtil.subBetween((String) value, "usec=", ",")))
                .build()));
        return respVO;
    }

    /**
     * 转换列表
     *
     * @param list 列表
     * @return {@code List<RedisKeyDefineRespVO>}
     */
    List<RedisKeyDefineRespVO> convertList(List<RedisKeyDefine> list);

}
